#include "rtc_op.h"
#include <FreeRTOS.h>
#include <timers.h>
#include <time.h>
#include "led_op.h"

static uint64_t rtc_time_stamp = 0;
static uint8_t time_available = 0;

static void rtc_inc(TimerHandle_t xTimer)
{
    rtc_time_stamp++;
}

void init_rtc(void)
{
    time_available = 0;
    rtc_time_stamp = 0;

    TimerHandle_t xTimer = xTimerCreate("RTC_1S_TICK", pdMS_TO_TICKS(1000), pdTRUE, NULL, rtc_inc);
    if (xTimer != NULL) {
        xTimerStart(xTimer, 0);
    }
}

void set_rtc_time_stamp(uint64_t time_stamp)
{
    rtc_time_stamp = time_stamp;
    time_available = 1;
    led_clear_status(LED_STAT_INVALID_TIME);
}

uint64_t get_rtc_time_stamp(void)
{
    // char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    // struct tm *p;
    // p=gmtime((time_t *)&rtc_time_stamp); /*变量t的值转换为实际日期时间的表示格式*/
    // printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
    // printf(" %s ", wday[p->tm_wday]);
    // printf("%02d:%02d:%02d\r\n", p->tm_hour, p->tm_min, p->tm_sec);

    return rtc_time_stamp;
}

uint8_t is_rtc_time_available(void)
{
    return time_available;
}
